package javanet.communication;

import java.util.ArrayList;
import java.util.List;

import javanet.context.JavaNETFacadeContext;
import javanet.listener.JavaNETCommunicationListener;
import javanet.util.BeanLocator;

import javax.ejb.Singleton;

/**
 * @author Markus Holzer
 *
 */
@Singleton
public class JavaNETCommunicationFactory {

	private Class<JavaNETCommunicationListener>[] listenerClassList;
	
	private Class<JavaNETFacadeContext> facadeContextClass;
	
	private String moduleName;
	
	@SuppressWarnings("unchecked")
	public void registerListener(Class<? extends JavaNETCommunicationListener>... listeners)
	{
		this.listenerClassList = (Class<JavaNETCommunicationListener>[])listeners;
		System.out.println(listeners.length + " Listeners registered");
	}
	
	@SuppressWarnings("unchecked")
	public void registerFacadeContext(Class<? extends JavaNETFacadeContext> context)
	{
		this.facadeContextClass = (Class<JavaNETFacadeContext>)context;
	}

	public Class<JavaNETCommunicationListener>[] getListenerClassList() {
		return listenerClassList;
	}

	public Class<JavaNETFacadeContext> getFacadeContextClass() {
		return facadeContextClass;
	}
	
	public List<JavaNETCommunicationListener> createRegisteredListeners() {
		List<JavaNETCommunicationListener> listenerList = new ArrayList<JavaNETCommunicationListener>();
		try {
			Class<JavaNETCommunicationListener>[] listeners = getListenerClassList();
			for (Class<? extends JavaNETCommunicationListener> listenerClass : listeners) {
				listenerList.add(listenerClass.newInstance());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return listenerList;
	}

	public String getModuleName() {
		return moduleName;
	}

	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
		BeanLocator.moduleName = moduleName;
	}

	
	
}
